import { defineConfig } from "vite";
import { resolve } from "path";
import uni from "@dcloudio/vite-plugin-uni";
import tailwindcss from "tailwindcss";
import autoprefixer from "autoprefixer";
// 小程序适配
import uniTailwind from "@uni-helper/vite-plugin-uni-tailwind";

import Components from "@uni-helper/vite-plugin-uni-components";
import { NutResolver } from "nutui-uniapp";

// 适配tailwindcss
const postcssPlugins = [tailwindcss(), autoprefixer()];

// https://vitejs.dev/config/
export default defineConfig({
  server: {
    //跨域配置
    host: "0.0.0.0",
    port: 5173,
    open: false, //是否自动打开浏览器
  },
  build: {
    minify: "terser",
    terserOptions: {
      compress: {
        drop_console: true,
      },
    },
    rollupOptions: {
      output: {
        chunkFileNames: "assets/js/[name]-[hash].js",
        entryFileNames: "assets/js/[name]-[hash].js",
        assetFileNames: "assets/[ext]/[name]-[hash].[ext]",
      },
    },
    //   关闭文件计算
    reportCompressedSize: false,
    //   关闭生成map文件 可以达到缩小打包体积
    sourcemap: false, // 这个生产环境一定要关闭，不然打包的产物会很大
  },
  // 路径配置
  resolve: {
    alias: [
      {
        find: "@",
        replacement: resolve(__dirname, "src"), //配置@ 路径
      },
    ],
  },
  plugins: [
    Components({
      resolvers: [NutResolver()],
    }),
    uni(),
    uniTailwind(),
  ],
  css: {
    postcss: {
      plugins: postcssPlugins,
    },
    preprocessorOptions: {
      scss: {
        additionalData: '@import "nutui-uniapp/styles/variables.scss";',
      },
    },
  },
});
